﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.UI;

namespace ASPNETVN.PORTAL
{
    public partial class ModuleTitle : UserControl
    {
        
        private string _AddUrl = string.Empty;
        private string _ManagerUrl = string.Empty;
        private string _StandardSettingUrl = string.Empty;
        private string _CustomSettingUrl = string.Empty;
        private string _PermissionUrl = string.Empty;

        [Browsable(true)]
        [Category("Appearance")]
        public string AddUrl
        {
            set { this._AddUrl = value; }
            get { return this._AddUrl; }
        }

        [Browsable(true)]
        [Category("Appearance")]
        public string ManagerUrl
        {
            set { this._ManagerUrl = value; }
            get { return this._ManagerUrl; }
        }

        [BrowsableAttribute(true)]
        [Category("Appearance")]
        public string StandardSettingUrl
        {
            set { this._StandardSettingUrl = value; }
            get { return this._StandardSettingUrl; }
        }

        [Browsable(true)]
        [Category("Appearance")]
        public string CustomSettingUrl
        {
            set { this._CustomSettingUrl = value; }
            get { return this._CustomSettingUrl; }
        }

        [Browsable(true)]
        [Category("Appearance")]
        public string PermissionUrl
        {
            set { this._PermissionUrl = value; }
            get { return this._PermissionUrl; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Obtain Portal from Current Context.
                Components.Configuration.Items.Portal portal = (Components.Configuration.Items.Portal)HttpContext.Current.Items["Portal"];

                // Obtain reference to parent portal module
                PortalModuleControl portalModule = (PortalModuleControl) this.Parent;

                //portalModule.Settings = Configuration.GetModuleSettings(portalModule.ModuleID);

                // Display Modular Title Text and Edit Buttons			
                ModuleTitleLabel.Text = portalModule.Module.Title;

                if (portalModule.IsAddable || portalModule.IsEditable || portalModule.IsDeleteable)
                    pnlTitle.Visible = true;
                else
                    pnlTitle.Visible = Convert.ToBoolean(portalModule.Settings["ShowTitle"].ToString());
                
                // Set up style of ModuleTitle
                if (string.IsNullOrEmpty(portalModule.Settings["TitleCssClass"].ToString()) == false)
                {
                    this.tblModuleTitle.Attributes.Add("class", portalModule.Settings["TitleCssClass"].ToString());
                }


                // Set up style of ModuleTitle's icon.

                tdIcon.Visible = Boolean.Parse(portalModule.Settings["ShowModuleTitleIcon"].ToString());

                if (string.IsNullOrEmpty(portalModule.Settings["IconImageUrl"].ToString()) == false)
                {
                    this.ImageModuleTitleIcon.ImageUrl = portalModule.Settings["IconImageUrl"].ToString();
                }

                // Display the Edit button if the parent portalmodule has configured the PortalModuleTitle User Control
                // to display it -- and the current client has edit access permissions
                if ((portal.AlwaysShowEditButton) || (PortalSecurity.IsInRoles("1")))
                {

                    if (this.AddUrl != null)
                    {                        
                        AddButton.ImageUrl = "~/images/icons/add.gif";
                        AddButton.NavigateUrl = this._AddUrl;
                        AddButton.ToolTip = Global.RM.GetString("SYSTEM_ADDNEW");
                        AddButton.Visible = PortalSecurity.HasAddAuthorization(portalModule.ModuleID);
                        if (AddButton.Visible) AddButton.Visible = this.AddUrl.Length > 0;
                    }
                    if (this.ManagerUrl != null)
                    {
                        ManagerButton.ImageUrl = "~/images/icons/Manager.gif";
                        ManagerButton.NavigateUrl = this._ManagerUrl;
                        ManagerButton.ToolTip = Global.RM.GetString("SYSTEM_MANAGER");
                        ManagerButton.Visible = PortalSecurity.HasManageAuthorization(portalModule.ModuleID);
                        if (ManagerButton.Visible) ManagerButton.Visible = this.ManagerUrl.Length > 0;
                    }

                    if (this.StandardSettingUrl != null)
                    {
                        StandardSettingButton.ImageUrl = "~/images/icons/Settings.gif";
                        StandardSettingButton.NavigateUrl = this._StandardSettingUrl;
                        StandardSettingButton.ToolTip = Global.RM.GetString("SYSTEM_SETTING");
                        StandardSettingButton.Visible = PortalSecurity.HasConfigureAuthorization(portalModule.ModuleID);
                    }

                    if (this.CustomSettingUrl != null)
                    {
                        CustomSettingButton.ImageUrl = "~/images/icons/Properties.gif";
                        CustomSettingButton.NavigateUrl = this._CustomSettingUrl;
                        CustomSettingButton.ToolTip = Global.RM.GetString("SYSTEM_PROPERTY");
                        CustomSettingButton.Visible = PortalSecurity.HasConfigureAuthorization(portalModule.ModuleID);                        
                    }

                    if (this.PermissionUrl != null)
                    {
                        PermissionButton.ImageUrl = "~/images/icons/Security.gif";
                        PermissionButton.NavigateUrl = this._PermissionUrl;
                        PermissionButton.ToolTip = Global.RM.GetString("SYSTEM_PERMISSION");
                        PermissionButton.Visible = PortalSecurity.HasSecureAuthorization(portalModule.ModuleID);
                    }
                }
            }
        }
    }
}